package cn.ytxy.ivos.user.service.impl;


import cn.ytxy.ivos.base.exception.ServiceException;
import cn.ytxy.ivos.base.response.StatusCode;
import cn.ytxy.ivos.user.mapper.UserMapper;
import cn.ytxy.ivos.user.pojo.dto.UserLoginParam;
import cn.ytxy.ivos.user.pojo.dto.UserQuery;
import cn.ytxy.ivos.user.pojo.dto.UserSaveParam;
import cn.ytxy.ivos.user.pojo.entity.User;
import cn.ytxy.ivos.user.pojo.vo.UserVO;
import cn.ytxy.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @ClassName UserServiceImpl
 * @Description TODO
 * @Author renhong~Deng
 * @Date 2025/4/23 9:37
 * @Version 1.0
 */

@Slf4j
/*@Service注解表示当前类为业务层实现类*/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper  userMapper;


    @Override
    public void saveUser(UserSaveParam userSaveParam) {
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        //如果没有id值,说明是新增操作
        if(user.getId()==null){
            //注意:员工的账号不是自己注册的!所以需要系统设置初始密码
            user.setPassword("123456");
            user.setCreateTime(new Date());
            userMapper.insert(user);
        }else{//有id,说明是更新操作
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }
    }

    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("登陆业务：userLoginParam={}",userLoginParam);
        UserVO userVO = userMapper.selectByUserName(userLoginParam.getUsername());
        if (userVO==null){
            //不能return需要抛出异常
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(userLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }

    @Override
    public List<UserVO> selectUser(UserQuery userQuery) {
        return userMapper.selectUser(userQuery);
    }

    @Override
    public void resetPassword(Long userId) {
        User user = new User();
        user.setId(userId);
        user.setCreateTime(new Date());
        user.setPassword("123456");
        userMapper.update(user);
    }

    @Override
    public void updateStatus(Long userId, String status) {
        log.debug("service层更新用户状态，userId={},status={}",userId,status);
        User user = new User();
        user.setId(userId);
        user.setCreateTime(new Date());
        user.setStatus(status);
        userMapper.update(user);
    }

    @Override
    public void deleteUser(Long userId) {
        log.debug("service层,删除用户，userId={}",userId);
        userMapper.delete(userId);
    }

    @Override
    public List<UserVO> selectAuditList(Long parentId) {
        log.debug("service层,查询审核人业务，parentId={}",parentId);
        //准备一个空的集合，用来保存查出来的多个审批人
        ArrayList<UserVO> userVOList = new ArrayList<>();
        UserVO auditUser1 = userMapper.selectById(parentId);
        userVOList.add(auditUser1);
        //如果直属领导也有上级，再把上上级查出来
        if (auditUser1!=null && auditUser1.getParentId()!=null){
            UserVO auditUser2 = userMapper.selectById(auditUser1.getParentId());
            userVOList.add(auditUser2);
        }
        return userVOList;
    }


}
